<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市名" prop="cityName">
        <el-select
          v-model="queryParams.cityName"
          placeholder="请选择城市名"
          filterable
          clearable
        >
          <el-option
            v-for="option in allCityList"
            :key="option.code"
            :label="option.completeName"
            :value="option.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:demand:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:demand:edit']"
          >修改</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:demand:remove']"
          >删除</el-button
        >
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          plain
          size="mini"
          @click="$router.push('/manage/demandPhone')"
          >手机号查看情况</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="demandList"
      @selection-change="handleSelectionChange"
    >
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="ID" align="center" prop="id" width="50" />
      <el-table-column label="需求标题" align="center" prop="demandTitle"
           width="200"
           show-overflow-tooltip
      />
      <el-table-column
        label="需求说明"
        align="center"
        prop="demandDesc"
        width="200"
        show-overflow-tooltip
      />
      <el-table-column
        label="需求类型"
        align="center"
        prop="demandType"
        width="80"
        show-overflow-tooltip
      >
        <template slot-scope="{ row }">
          {{
            row.demandType === 1
              ? "机柜"
              : row.demandType === 2
              ? "带宽"
              : row.demandType === 4
              ? "算力"
              : row.demandType === 5
              ? "硬件"
              : "算力服务器"
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="省"
        align="center"
        prop="provinceName"
        width="80"
      />
      <el-table-column label="市" align="center" prop="cityName" width="80" />

      <el-table-column
        label="需求预算（元）"
        align="center"
        prop="budgetPrice"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="联系人"
        align="center"
        prop="contactUser"
        width="100"
      />
      <el-table-column
        label="联系电话"
        align="center"
        prop="contactUserMobile"
        width="120"
      />
      <el-table-column label="需求时间" align="center" width="240">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.demandBegin, "{y}-{m}-{d}") }}</span> ~
          <span>{{ parseTime(scope.row.demandEnd, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center" />

      <el-table-column label="状态" align="center" prop="status" width="180">
        <template slot-scope="{ row }">
          <span v-if="row.status === -1">未审核</span>
          <span v-else-if="row.status === -2">已驳回</span>
          <template v-else>
            <el-switch
              :value="row.status"
              :active-value="1"
              :inactive-value="0"
              @change="toggleStatus(row)"
            ></el-switch>
            <span>{{ row.status ? "上架" : "下架" }}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="140"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleToggleIsTop(scope.row)"
            >{{ scope.row.recommendStatus ? "取消置顶" : "置顶" }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:demand:edit']"
            >修改</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:demand:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改需求信息对话框 -->
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="open"
      width="700px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="需求类型" prop="demandType">
          <el-select
            v-model="form.demandType"
            placeholder="请选择需求类型"
            clearable
            @change="typeChange"
          >
            <el-option :value="1" label="机柜"></el-option>
            <el-option :value="2" label="带宽"></el-option>
            <el-option :value="4" label="算力"></el-option>
            <el-option :value="5" label="硬件"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="需求标题" prop="demandTitle">
          <el-input v-model="form.demandTitle" placeholder="请输入需求标题" />
        </el-form-item>
        <el-form-item
          v-if="form.demandType===5"
          label="品牌"
          prop="brandName">
          <el-input v-model="form.brandName" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item
          v-if="form.demandType===5"
          label="型号"
          prop="modelName">
          <el-input v-model="form.modelName" placeholder="请输入型号" />
        </el-form-item>

        <el-form-item label="需求说明" prop="demandDesc">
          <el-input
            v-model="form.demandDesc"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="省市" prop="provinceArr">
          <el-cascader
            style="width: 100%"
            v-model="form.provinceArr"
            :options="provinceOptions"
            :props="cascaderProps"
          ></el-cascader>
        </el-form-item>
<!--        <el-form-item label="需求预算单位" prop="priceUnit">-->
<!--          <el-select v-model="form.priceUnit" placeholder="请选择单位">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type[priceUnitDictField]"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="需求预算（元）" prop="budgetPrice">
          <el-input v-model="form.budgetPrice" placeholder="请输入需求预算" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactUser">
          <div class="flex w-full items-center">
            <el-input
              v-model="form.contactUser"
              placeholder="请输入联系人"
            ></el-input>
          </div>
        </el-form-item>
        <el-form-item label="联系电话" prop="contactUserMobile">
          <div class="flex w-full items-center">
            <el-input
              v-model="form.contactUserMobile"
              placeholder="请输入联系电话"
              :maxlength="11"
            ></el-input>
          </div>
        </el-form-item>
        <el-form-item label="需求时间-起" prop="demandBegin">
          <el-date-picker
            clearable
            v-model="form.demandBegin"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择需求时间-起"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="需求时间-止" prop="demandEnd">
          <el-date-picker
            clearable
            v-model="form.demandEnd"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择需求时间-止"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDemand,
  getDemand,
  delDemand,
  addDemand,
  updateDemand,
  toggleDemand,
  toggleDemandIsTop,
} from "@/api/system/demand";

import provinceMixin from "./mixin/province";

export default {
  name: "Demand",
  mixins: [provinceMixin],
  dicts: ["frame_price_unit", "bw_price_unit", "gpu_price_unit"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 需求信息表格数据
      demandList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        demandTitle: null,
        demandDesc: null,
        demandType: null,
        cityName: null,
        budgetPrice: null,
        brandName:null,
        modelName:null,
        demandBegin: null,
        demandEnd: null,
        auditStatus: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        demandType: [{ required: true, message: "请选择需求类型" }],
        demandTitle: [{ required: true, message: "请输入需求标题" }],
        demandDesc: [{ required: true, message: "请输入需求说明" }],
        brandName: [{ required: true, message: "请输入品牌" }],
        modelName: [{ required: true, message: "请输入型号" }],
        provinceArr: [{ required: true, type: "array", message: "请选择省市" }],
        budgetPrice: [{ required: true, message: "请输入需求预算" }],
        demandBegin: [{ required: true, message: "请选择需求开始时间" }],
        demandEnd: [{ required: true, message: "请选择需求结束时间" }],
        contactUser: [{ required: true, message: "请输入联系人" }],
        contactUserMobile: [{ required: true, message: "请输入联系电话" }],
      },
    };
  },
  computed: {
    priceUnitDictField() {
      return this.form.demandType === 1
        ? "frame_price_unit"
        : this.form.demandType === 2
        ? "bw_price_unit"
        : this.form.demandType === 4
        ? "gpu_price_unit"
        : this.form.demandType === 5
        ? "gpu_price_unit"
        : "";
    },
  },
  created() {
    this.getList();
  },
  methods: {
    typeChange() {
      this.form.priceUnit = null;
    },
    async toggleStatus(row) {
      const { id, status } = row;
      const { code } = await toggleDemand({
        id,
        status: Number(!status),
      });
      if (code === 200) {
        this.$message.success(status ? "下架成功" : "上架成功");
        this.$set(row, "status", Number(!status));
      }
    },
    /** 查询需求信息列表 */
    getList() {
      this.loading = true;
      listDemand(this.queryParams).then((response) => {
        this.demandList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        demandId: null,
        demandTitle: null,
        demandDesc: null,
        demandType: null,
        cityName: null,
        budgetPrice: null,
        brandName:null,
        modelName:null,
        demandBegin: null,
        demandEnd: null,
        auditStatus: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        contactUser: null,
        contactUserMobile: null,
        orderNum: null,
        priceUnit: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.demandId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加需求信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const demandId = row.id || this.ids;
      getDemand(demandId).then((response) => {
        this.form = response.data;
        this.form.provinceArr = [
          response.data.provinceName,
          response.data.cityName,
        ];
        this.open = true;
        this.title = "修改需求信息";
      });
    },
    async handleToggleIsTop(row) {
      await this.$confirm(
        `确定${row.recommendStatus ? "取消置顶" : "置顶"}该数据吗？`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      );
      await toggleDemandIsTop({
        id: row.id,
        status: Number(!row.recommendStatus),
      });
      this.$message.success(`${row.recommendStatus ? "取消置顶" : "置顶"}成功`);
      this.getList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.provinceName = this.form.provinceArr[0];
          this.form.cityName = this.form.provinceArr[1];
          if (this.form.id != null) {
            updateDemand(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDemand(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const demandIds = row.demandId || this.ids;
      this.$modal
        .confirm('是否确认删除需求信息编号为"' + demandIds + '"的数据项？')
        .then(function () {
          return delDemand(demandIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/demand/export",
        {
          ...this.queryParams,
        },
        `demand_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
